using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Framework.Library.Components.Security;
using Framework.Library.Components.Commands;
using Framework.Library;
using Framework.Library.Utilities.Database;
using Framework.Library.Components.Database;
using Framework.Library.Utilities;

namespace Framework.Portal.Administrator.Database
{
    public partial class StoreNew : AuthenticatedControl
    {        
        _StoredProcedure objProc = new _StoredProcedure();
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((!CurrentUser.IsSuperAdmin && CommandController.IsSystemModule(AppEnv.CommandID())) || !CommandController.IsAvailableForUser(AppEnv.CommandID(), CurrentUser.UserID, Convert.ToInt32(AppEnv.PortalId())))
                Response.Redirect(AppEnv.ADMIN_CMD, true);
            if (!IsPostBack)
            {
                lblCommandName.Text = AppEnv.CommandName();
                dropOwner.DataSource = Info.GetOwners();
                dropOwner.DataValueField = "name";
                dropOwner.DataTextField = "name";
                dropOwner.DataBind();

                Info.CheckDropList(dropOwner, "dbo");
            }
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            try
            {
                objProc.Create(txtDefinition.Text);
                Response.Redirect(AppEnv.AdminUrlParams(CommandController.GetCommandbyID((int)PortalControls.Store)));
            }
            catch (Exception ex)
            {
                lblError.Visible = true;
                lblError.Text = "<p>" + ex.Message + "</p>";
            }
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            string strDefinition = "";

            strDefinition += "CREATE PROCEDURE [" + dropOwner.SelectedValue + "].[" + txtName.Text + "]";
            if (txtNumber.Text != "")
            {
                strDefinition += ";" + txtNumber.Text;
            }

            switch (radioOptions.SelectedValue)
            {
                case "":
                    if (chkEncription.Checked)
                    {
                        strDefinition += "\nWITH ENCRYPTION\n";
                    }
                    break;
                case "RECOMPILE":
                    if (chkEncription.Checked)
                    {
                        strDefinition += "\nWITH RECOMPILE, ENCRYPTION\n";
                    }
                    else
                    {
                        strDefinition += "\nWITH RECOMPILE\n";
                    }
                    break;
                case "FOR REPLICATION":
                    if (chkEncription.Checked)
                    {
                        strDefinition += "\nWITH ENCRYPTION\nFOR REPLICATION";
                    }
                    else
                    {
                        strDefinition += "\nFOR REPLICATION";
                    }
                    break;
            }

            strDefinition += "\nAS\n\tBEGIN\n\t\t\n\tEND";
            txtDefinition.Text = strDefinition;


            pnlProcCode.Visible = true;
            pnlOpt.Visible = false;
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            pnlOpt.Visible = true;
            pnlProcCode.Visible = false;
        }
    }
}